<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="s_content" label="关键字">
          <el-input v-model="queryParams.s_content" placeholder="日志内容" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table v-loading="loading" :data="pageData" highlight-current-row border>
        <el-table-column label="id" align="center" width="80" prop="id" />
        <el-table-column label="unicode" align="center" prop="unicode" />
        <el-table-column label="创建时间" align="center" width="180">
          <template #default="scope">
            <span v-if="scope.row.created_at > 0">
              {{ formatTimestamp(scope.row.created_at * 1000) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" width="180">
          <template #default="scope">
            <span v-if="scope.row.updated_at > 0">
              {{ formatTimestamp(scope.row.updated_at * 1000) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button v-hasPerm="'sys:log:info'" type="primary" icon="Document" link size="small"
              @click="handleOpenDialog(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.page" v-model:limit="queryParams.ps"
        @pagination="handleQuery" />
    </el-card>

    <el-dialog :close-on-click-modal="false" v-model="visible" title="详情" append-to-body @close="handleCloseDialog">
      <div>{{ content }}</div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseDialog">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "Log",
  inheritAttrs: false,
});

import LogAPI, { LogPageQuery, LogPageVO } from "@/api/system/log";
import { formatTimestamp } from '@/utils/index';

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<LogPageQuery>({
  page: 1,
  ps: 10,
  s_content: "",
  createTime: ["", ""],
});

// 日志表格数据
const pageData = ref<LogPageVO[]>();

/** 查询 */
function handleQuery() {
  loading.value = true;
  LogAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.data;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  queryParams.createTime = undefined;
  handleQuery();
}

// 打开弹窗
const visible = ref(false);
const content = ref('');
const handleOpenDialog = (data: any) => {
  visible.value = true;
  content.value = data.content;
}

// 关闭弹窗
const handleCloseDialog = () => {
  visible.value = false;
}

onMounted(() => {
  handleQuery();
});
</script>
